new Vue({
    el:"#app",
    data: {
        fullscreenLoading:false,
        screenLoading:false,
        productData:[],
        id:'',
        dialogProductVisible: false,
        dialogManageVisible:false,
        dialogCateVisible:false,
        dialogDownloadVisible:false,
        queryContent:{
            keyword:'',
            productStatus:'',
            managerStatus:'1',
            date:[]
        },
        pageNo: 1,
        pageSize: 20,
        total: 0,
        pages: 0,
        productId:0,
        productStatus: 0,
        manageStatus: 0,
        downloadStatus: 1,
        editable:[],
        orderNumTemp:null,
        allCate:[],
        selectCateId:[]
    },
    mounted:function(){
        var _this=this;
        _this.loadProductList();
    },
    methods:{
        trim:function(str){
            if(isNaN(str)){
                return str.replace(/(^\s*)|(\s*$)/g, "");
            }else{
                return str
            }
        },
        loadProductList:function () {
            var _this = this;
            _this.screenLoading=true;
            var data=new URLSearchParams();
            data.append("pageSize",_this.pageSize);
            data.append("currentPage",_this.pageNo);
            data.append("keyword",_this.trim(_this.queryContent.keyword));
            data.append("productFlag",_this.queryContent.productStatus);
            data.append("managerFlag",_this.queryContent.managerStatus);
            var startDate = '';
            var endDate = '';
            if(_this.queryContent.date != null && _this.queryContent.date[0] != null) {
                startDate = _this.easyFormatDate(_this.queryContent.date[0]);
                endDate = _this.easyFormatDate(_this.queryContent.date[1]);
            }
            if(startDate != null && startDate != "") {
                data.append("startDate",startDate);
            }
            if(endDate != null && endDate != "") {
                data.append("endDate",endDate);
            }
            axios.post(request_prefix+"/cash/product/list",data)
                .then(function(res){
                    if(res.data.code===0){
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.productData = res.data.data.dataList;
                        _this.editable = new Array(_this.productData.length);
                        _this.screenLoading = false;
                        _this.loadAllCate();
                        return;
                    }else{
                        _this.screenLoading=false;
                        _this.$message.error("获取数据失败");
                        return;
                    }
                })
                .catch(function(res){
                    _this.screenLoading=false;
                    _this.$message.error("请求失败");
                    return;
                })
        },
        resetQuery:function () {
            var _this = this;
            _this.queryContent.keyword = '';
            _this.queryContent.productStatus = '';
            _this.queryContent.managerStatus = '';
            _this.queryContent.date = [];
            _this.pageNo = 1;
            _this.loadProductList();
        },
        handleSizeChange:function (val) {
            var _this = this;
            _this.pageSize = val;
            _this.handleCurrentChange(1);
        },
        //分页
        handleCurrentChange:function(val) {
            var _this=this;
            var data=new URLSearchParams();
            data.append("pageSize",_this.pageSize);
            data.append("currentPage",val);
            data.append("keyword",_this.trim(_this.queryContent.keyword));
            data.append("productFlag",_this.queryContent.productStatus);
            data.append("managerFlag",_this.queryContent.managerStatus);
            var startDate = '';
            var endDate = '';
            if(_this.queryContent.date != null  && _this.queryContent.date[0] != null) {
                startDate = _this.easyFormatDate(_this.queryContent.date[0]);
                endDate = _this.easyFormatDate(_this.queryContent.date[1]);
            }
            if(startDate != null && startDate != "") {
                data.append("startDate",startDate);
            }
            if(endDate != null && endDate != "") {
                data.append("endDate",endDate);
            }
            axios.post(request_prefix+"/cash/product/list",data)
                .then(function(res){
                    if(res.data.code===0){
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.productData = res.data.data.dataList;
                        _this.editable = new Array(_this.productData.length);
                        _this.loadAllCate();
                    }else{
                        _this.$message.error("请求数据失败")
                    }
                })
                .catch(function(res){
                    _this.$message.error("请求失败")
                })
        },
        toProductDetail:function (id) {
            localStorage.setItem("product_id_",id);
            window.location.href = request_prefix+"/cash/product/detailPage";
        },
        toProductPv:function (id) {
            localStorage.setItem("product_id_",id);
            window.location.href = request_prefix+"/cash/product/pvPage";
        },
        toNewPage:function () {
            window.location.href = request_prefix+"/cash/product/createPage";
        },
        toEditProduct:function (id) {
            localStorage.setItem("product_id_",id);
            window.location.href = request_prefix+"/cash/product/editPage";
        },
        easyFormatDate: function(date) {
            var year = date.getFullYear();
            var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1 + "";
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate() + "";
            return year + "-" + month + "-" + day;
        },
        changeProductStatusDialog:function (id,status) {
            this.productId = id;
            this.productStatus = status;
            this.dialogProductVisible = true;
        },
        productConfirmChange:function (id,status) {
            var _this = this;
            var data=new URLSearchParams();
            data.append("productId",parseInt(id));
            data.append("productFlag",parseInt(status));
            axios.post(request_prefix+"/cash/product/updateProductStatus",data)
                .then(function(res){
                    if(res.data.code===0){
                        _this.productId = null;
                        _this.productStatus = null;
                        _this.dialogProductVisible = false;
                        _this.loadProductList();
                    }else{
                        _this.$message.error("更新失败")
                    }
                })
        },
        changeManageStatusDialog:function (id,status) {
            this.productId = id;
            this.manageStatus = status;
            this.dialogManageVisible = true;
        },
        manageConfirmChange:function (id,status) {
            var _this = this;
            var data = new URLSearchParams();
            data.append("productId",parseInt(id));
            data.append("manageFlag",parseInt(status));
            axios.post(request_prefix+"/cash/product/updateManageStatus",data)
                .then(function(res){
                    if(res.data.code===0){
                        _this.productId = null;
                        _this.manageStatus = null;
                        _this.dialogManageVisible = false;
                        _this.loadProductList();
                    }else{
                        _this.$message.error("更新失败")
                    }
                })
        },
        changeDownloadStatusDialog:function (id,status) {
            this.productId = id;
            this.downloadStatus = status + '';
            this.dialogDownloadVisible = true;
        },
        downloadConfirmChange:function (id,status) {
            var _this = this;
            var data = new URLSearchParams();
            data.append("productId",parseInt(id));
            data.append("downloadFlag",parseInt(status));
            axios.post(request_prefix+"/cash/product/updateDownloadStatus",data)
                .then(function(res){
                    if(res.data.code===0){
                        _this.productId = null;
                        _this.downloadStatus = null;
                        _this.dialogDownloadVisible = false;
                        _this.loadProductList();
                    }else{
                        _this.$message.error("更新失败")
                    }
                })
        },
        productFilter:function (productFlag) {
            var productStatusName = '';
            switch(parseInt(productFlag)) {
                case 0: productStatusName = '新品'; break;
                case 1: productStatusName = '力荐'; break;
                case 2: productStatusName = '热销'; break;
                case 3: productStatusName = '推荐'; break;
                default:
                    productStatusName = "无状态";
                    break;
            }
            return productStatusName;
        },
        manageFilter:function (manageFlag) {
            var manageStatusName = '';
            switch(parseInt(manageFlag)) {
                case 0: manageStatusName = '未审核'; break;
                case 1: manageStatusName = '已审核'; break;
                case 2: manageStatusName = '已停用'; break;
                default:manageStatusName = "无状态"; break;
            }
            return manageStatusName;
        },
        downloadFilter:function (downloadFlag) {
            var downloadStatusName = '';
            switch(parseInt(downloadFlag)) {
                case 0: downloadStatusName = '不可下载'; break;
                case 1: downloadStatusName = '可下载'; break;
                default:downloadStatusName = "不确定"; break;
            }
            return downloadStatusName;
        },
        changeNum:function(row){
            var _this = this;
            _this.editable[row] = true;
            _this.orderNumTemp = _this.productData[row].orderNum;
            _this.$set(_this.editable,row,true);
        },
        putOrderNum:function (row) {
            var _this = this;
            var patrn = /^[+-]?\d+(\.\d+)?$/;
            if(!patrn.test(_this.orderNumTemp)) {
                _this.$message.warning("请输入数字");
                return;
            }
            if(_this.orderNumTemp < 0) {
                _this.$message.warning("排序值不能为负数");
                return;
            }
            if(parseInt(_this.orderNumTemp) == parseInt(_this.productData[row].orderNum)) {
                _this.editable[row] = false;
                _this.$set(_this.editable,row,false);
                return;
            }
            var data = new URLSearchParams();
            data.append("id",_this.productData[row].id);
            data.append("orderNum",_this.orderNumTemp);
            axios.post(request_prefix + "/cash/product/orderNum",data)
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.productData[row].orderNum = _this.orderNumTemp;
                        _this.productData.sort(function (o1,o2) {
                            var order1 = parseInt(o1.orderNum);
                            var order2 = parseInt(o2.orderNum);
                            if(order1 < order2) {
                                return 1;
                            } else if(order1 > order2) {
                                return -1;
                            } else {
                                return 0;
                            }
                        });
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                });
            _this.editable[row] = false;
            _this.$set(_this.editable,row,false);
        },
        loadAllCate:function () {
            var _this = this;
            axios.get(request_prefix+'/cash/cate/list')
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.allCate = res.data.data;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
                });
        },
        changeCateDialog:function (id,cateList) {
            var _this = this;
            _this.selectCateId = [];
            _this.productId = id;
            for (var i = 0;i < cateList.length;i++) {
                _this.selectCateId[i] = cateList[i].id;
            }
            _this.dialogCateVisible = true;
        },
        cateConfirmChange:function (productId) {
            var _this = this;
            if(_this.selectCateId.length == 0) {
                _this.$message.warning("请选择至少一个类别");
                return;
            }
            var cateProductId = {};
            cateProductId.productId = productId;
            cateProductId.cateIdList = _this.selectCateId;
            axios.post(request_prefix + "/cash/cate/bindProductAndCate",cateProductId)
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.loadProductList();
                        _this.dialogCateVisible = false;
                    } else {
                        _this.$message.error(res.data.message);
                        _this.dialogCateVisible = false;
                    }
                })
                .catch(function () {
                    _this.$message.error("请求过程中产生错误");
                })
        },
        toCateList:function () {
            window.location.href = request_prefix + "/cash/cate/listPage";
        }
    }
});